﻿/******************************************************************************

    Copyright (c) 2010, Sergey Mudrov
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
        * Redistributions of source code must retain the above copyright
          notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright
          notice, this list of conditions and the following disclaimer in the
          documentation and/or other materials provided with the distribution.
        * Neither the name of Sergey Mudrov nor the
          names of its contributors may be used to endorse or promote products
          derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL SERGEY MUDROV BE LIABLE FOR ANY
    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using VkApi.DataTypes;

namespace VkApi
{
    /// <summary>
    /// Class that represents external objects.
    /// </summary>
    public class VkExternal
    {
        private ScriptObject _external = null;
        private VkScriptable _vkScriptable = null;

        /// <summary>
        /// Occurs when application is added.
        /// </summary>
        public event EventHandler ApplicationAdded;

        /// <summary>
        /// Occurs when settings are changed.
        /// </summary>
        public event SettingsChanged_Handler SettingsChanged;

        /// <summary>
        /// Occurs when balance is changed.
        /// </summary>
        public event BalanceChanged_Handler BalanceChanged;

        /// <summary>
        /// Occurs when window is blured.
        /// </summary>
        public event EventHandler WindowBlur;

        /// <summary>
        /// Occurs when on window focus.
        /// </summary>
        public event EventHandler WindowFocus;

        /// <summary>
        /// 
        /// </summary>  
        internal VkExternal(ScriptObject vk, VkScriptable vkScriptable)
        {
#if DEBUG
            Trace.WriteLine("VkExternal: Try found js External object..."); 
#endif

            this._external = vk.GetProperty("External") as ScriptObject;
            if (this._external == null)
            {
#if DEBUG
                Trace.WriteLine("VkExternal: External object not found."); 
#endif

                throw new Exception("VkExternal: External object not found.");
            }

            this._vkScriptable = vkScriptable;

            this._vkScriptable.ApplicationAdded += new EventHandler(VkScriptable_ApplicationAdded);
            this._vkScriptable.BalanceChanged += new BalanceChanged_Handler(VkScriptable_BalanceChanged);
            this._vkScriptable.SettingsChanged += new SettingsChanged_Handler(VkScriptable_SettingsChanged);
            this._vkScriptable.WindowBlur += new EventHandler(VkScriptable_WindowBlur);
            this._vkScriptable.WindowFocus += new EventHandler(VkScriptable_WindowFocus);

#if DEBUG
            Trace.WriteLine("VkExternal: Init succeeded"); 
#endif
        }

        #region Public interface.

        /// <summary>
        /// Shows the install box.
        /// </summary>
        public void ShowInstallBox()
        {
            this._external.Invoke("showInstallBox");
        }

        /// <summary>
        /// Shows the settings box.
        /// </summary>
        public void ShowSettingsBox()
        {
            this._external.Invoke("showSettingsBox", 0);
        }

        /// <summary>
        /// Shows the settings box.
        /// </summary>
        /// <param name="userSettings">The user settings.</param>
        public void ShowSettingsBox(UserSettings userSettings)
        {
            this._external.Invoke("showSettingsBox", (int)userSettings);
        }

        /// <summary>
        /// Shows the payment box.
        /// </summary>
        public void ShowPaymentBox()
        {
            this._external.Invoke("showPaymentBox", 0);
        }

        /// <summary>
        /// Shows the payment box.
        /// </summary>
        /// <param name="votes">The votes.</param>
        public void ShowPaymentBox(int votes)
        {
            this._external.Invoke("showPaymentBox", votes);
        }

        /// <summary>
        /// Sets the title.
        /// </summary>
        /// <param name="title">The title.</param>
        public void SetTitle(string title)
        {
            this._external.Invoke("setTitle", title);
        }

        /// <summary>
        /// Shows the invite box.
        /// </summary>
        /// <added_by>Alexey Serafimov</added_by>
        public void ShowInviteBox()
        {
            this._external.Invoke("showInviteBox");
        }

        /// <summary>
        /// Opens a window to buy through registered e-shop. (Beta) No order parameters.
        /// </summary>
        /// <param name="param1">The title.</param>
        public void ShowMerchantPaymentBox(Object param1)
        {
            this._external.Invoke("showMerchantPaymentBox", param1);
        }

        /// <summary>
        /// Saves the post.
        /// </summary>
        /// <param name="hash">The hash.</param>
        public void SaveWallPost(string hash)
        {
            this._external.Invoke("saveWallPost", hash);
        }

        /// <summary>
        /// Sets the location.
        /// </summary>
        /// <param name="location">The location.</param>
        /// <param name="fire_event">if set to <c>true</c> event will invoke.</param>
        public void SetLocation(string location, bool fire_event)
        {
            this._external.Invoke("setLocation",location,fire_event);
        }

        /// <summary>
        /// Scrolls the window.
        /// </summary>
        /// <param name="top">The scroll offset from the top 0 coordinate. By default: 0.</param>
        /// <param name="speed">The speed of scroll animation in milliseconds. Be default: 0.</param>
        public void ScrollWindow(int top, int speed)
        {
            this._external.Invoke("scrollWindow", top, speed);
        }

        /// <summary>
        /// Resizes the window.
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        public void ResizeWindow(int width, int height)
        {
            this._external.Invoke("resizeWindow", width, height);
        }

        #endregion

        #region Internal implementation.

        /// <summary>
        /// 
        /// </summary>  
        private void VkScriptable_WindowFocus(object sender, EventArgs e)
        {
            if (this.WindowFocus != null)
                this.WindowFocus(this, EventArgs.Empty);
        }

        /// <summary>
        /// 
        /// </summary>  
        private void VkScriptable_WindowBlur(object sender, EventArgs e)
        {
            if (this.WindowBlur != null)
                this.WindowBlur(this, EventArgs.Empty);
        }

        /// <summary>
        /// 
        /// </summary>  
        private void VkScriptable_SettingsChanged(UserSettings settings)
        {
            if (this.SettingsChanged != null)
                this.SettingsChanged(settings);
        }

        /// <summary>
        /// 
        /// </summary>  
        private void VkScriptable_BalanceChanged(int balance)
        {
            if (this.BalanceChanged != null)
                this.BalanceChanged(balance);
        }

        /// <summary>
        /// 
        /// </summary>  
        private void VkScriptable_ApplicationAdded(object sender, EventArgs e)
        {
            if (this.ApplicationAdded != null)
                this.ApplicationAdded(this, EventArgs.Empty);
        } 

        #endregion

    }
}
